package logic

import (
	"context"
	"errors"
	"furni-mgr/furnirpc/furnirpc"
	"furni-mgr/furnirpc/internal/svc"
	"furni-mgr/models/mysql"
	"github.com/zeromicro/go-zero/core/logx"
)

type FurnitureAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurnitureAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurnitureAddLogic {
	return &FurnitureAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 家具添加方法
func (l *FurnitureAddLogic) FurnitureAdd(in *furnirpc.FurnitureAddRequest) (*furnirpc.FurnitureAddResponse, error) {
	// todo: add your logic here and delete this line

	brand := mysql.Brand{
		BrandName: in.BrandName,
	}

	err := brand.FindBrandByName(mysql.DB, in.BrandName)
	if err != nil {
		return nil, errors.New("品牌查询失败")
	}

	class := mysql.Class{
		ClassName: in.ClassName,
	}

	err = class.FindClassByName(mysql.DB, in.ClassName)
	if err != nil {
		return nil, errors.New("类型查询失败")
	}

	furniture := mysql.Furniture{
		FurnitureName:  in.FurnitureName,
		FurniturePrice: float64(in.FurniturePrice),
		BrandId:        int64(brand.ID),
		ClassId:        int64(class.ID),
	}

	err = furniture.FurnitureAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("添加失败")
	}

	return &furnirpc.FurnitureAddResponse{
		FurnitureId: int64(furniture.ID),
	}, nil
}
